package at.ac.tuwien.sepm.s2011.e0208329.dao;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
//import org.junit.AfterClass;
import org.junit.Before;
//import org.junit.BeforeClass;
import org.junit.Test;

import at.ac.tuwien.sepm.s2011.e0208329.db.DatabaseManager;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;

public class DienstleistungDAOImplTest {
	private DatabaseManager dbm;
	private DienstleistungDAOImpl serviceDAO;
	private static final Logger log = Logger.getLogger(DienstleistungDAOImpl.class);
	
	@Before
	public void setUp() throws Exception { // initialize
		PropertyConfigurator.configure("log4j.properties");
		dbm = DatabaseManager.getDatabaseManager(); //initialize dbm
		dbm.getConnection().setAutoCommit(false); // set the auto-commit of connection to false, needed for rollback()
		serviceDAO = DAOFactoryImpl.getDAOFactory().getServiceDAO();
	}

	@After
	public void tearDown() throws Exception {//roll back
		dbm.getConnection().rollback();
	}

	@Test(expected = IllegalArgumentException.class)
	public void testInsertService_shouldThrowExp() throws SQLException {
		Dienstleistung service = null;
		serviceDAO.insertService(service);
	}
	
	@Test
	public void testDeleteService() throws SQLException{
		serviceDAO.deleteService(1);
		String sql = "SELECT deleted from Dienstleistung WHERE iddienstleistung=1";
		ArrayList<ArrayList<String>> rs = dbm.query(sql);
		boolean deleted = false;
		for (ArrayList<String> arrl : rs) { // due to the unique property of "name", only one Service will be read
			deleted = Boolean.parseBoolean(arrl.get(0));
			log.debug(">>>>>>"+ Boolean.parseBoolean(arrl.get(0))); // transfer the first String to Int as ID
		}
		assertEquals(true,deleted);
	}
	
	
	/**
	 * combo-test for InsertService() getService() and getServiceName()
	 * @throws SQLException
	 */
	@Test
	public void test_InsertService_getServiceID_getServiceName() throws SQLException{
		Dienstleistung service = new Dienstleistung("xxx",20,0.2,"SL",3);
		serviceDAO.insertService(service);
		int id = serviceDAO.getServiceID("xxx");
		String name =  serviceDAO.getSerivceName(id);
        assertEquals("xxx",name);		
	}
	
	
	@Test
	public void testSetSaleNumber1() throws SQLException{
		int a= serviceDAO.setSaleNumber(2,2); // all services' saleNo should be 0, 0+2=0
		assertEquals(2,a);

	}
	
	@Test
	public void testSetSaleNumber2() throws SQLException{
		int a=serviceDAO.setSaleNumber(999,0); // 
		assertEquals(0,a);
	}

	/**
	 * Test if all services can be shown, when the database not null
	 * @throws Exception Any kind of exception that is thrown will make the test failed
	 */
	@Test
	public void testShowAllServices() throws Exception {
		ArrayList<Dienstleistung> myServices = serviceDAO.getAllServices();
		assertNotNull(myServices);
		assertTrue(myServices.size() > 0 );
	}
	
	/**
	 * All services are deleted. 
	 * The number of services should equal 0 and this was not caused by a database access problem. 
	 * @throws Exception Any kind of exception that is thrown will make the test failed
	 */
	@Test
	public void testShowAllServices_NonAvailable() throws Exception {
		String sqlStmt = "DELETE FROM dienstleistung"; 
		dbm.update(sqlStmt);                          // empty the Service Table
		ArrayList<Dienstleistung> myServices = serviceDAO.getAllServices();
		assertEquals(0, myServices.size());
	}
	


}
